<script lang="ts" setup>
import { getCurrentInstance, defineComponent, ref } from 'vue'
import GlobalApi from '../../api/global';
import { LoginBean } from '../../Bean/LoginBean';
import { WxResponse } from '../../util/http/axois/AxiosInterface';
import storage from '../../util/storage';
// import { getConsoleInfo } from '../../api/dashboard/console';
const username = ref('');
const password = ref('');
const { proxy }  = getCurrentInstance();
const onSubmit = (val) => {
  // getConsoleInfo()
  proxy.$toast.loading({
    duration: 0,
    message: '正在登录',
    forbidClick: true
  })
  try {
    GlobalApi().toLogin({ phone: username.value, password: password.value, sys: 2 }).then((res: WxResponse<LoginBean>) => {
      if (res.code > 200) {
        proxy.$toast.clear()
        proxy.$toast(res.result)
        return
      }
      const token = res.result.token
      storage().set('token', token)
      proxy.$toast.clear()
      proxy.$router.push('/')
    }).catch(e => {
      console.error('登录出错u', e)
      proxy.$toast.clear()
      proxy.$toast('登录失败')
    })
  } catch (e) {
    throw e
  }
}
</script>
<template>
  <div class="login-container">
    <div class="header">
      <div class="logo">
        <img src="@/assets/wxlogo.jpg" alt="">
      </div>
    </div>
    <div class="main">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container{
  padding: 32px;
}
</style>
